package com.gz.EuchreU.Helpers;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.font.FontManager;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureManager;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;

import com.gz.EuchreU.Contants.CardValues;
import com.gz.EuchreU.Contants.Suits;
import com.gz.EuchreU.Contants.UI;

public class Textures {

	public static final int TITLE_DECK_TEXTURE = 0;
	public static final int TITLE_EUCHRE_TEXTURE = TITLE_DECK_TEXTURE +1;
	public static final int TITLE_EUCHRE_CLEAR_TEXTURE = TITLE_EUCHRE_TEXTURE + 1;
	public static final int NEW_GAME_TEXTURE = TITLE_EUCHRE_CLEAR_TEXTURE + 1;
	public static final int SETTINGS_TEXTURE = NEW_GAME_TEXTURE + 1;
	public static final int EXIT_TEXTURE = SETTINGS_TEXTURE + 1;
	public static final int BUTTON_TEXTURE = EXIT_TEXTURE + 1;
	public static final int TABLE_BLACK_TEXTURE = BUTTON_TEXTURE + 1;
	public static final int DIAMOND_TEXTURE = TABLE_BLACK_TEXTURE + 1;
	public static final int CLUB_TEXTURE = DIAMOND_TEXTURE + 1;
	public static final int HEART_TEXTURE = CLUB_TEXTURE + 1;
	public static final int SPADE_TEXTURE = HEART_TEXTURE + 1;
	public static final int PICK_UP_TEXTURE = SPADE_TEXTURE + 1;
	public static final int PASS_TEXTURE = PICK_UP_TEXTURE + 1;
	public static final int DEALER_TEXTURE = PASS_TEXTURE + 1;
	public static final int GREEN_GLASS_TEXTURE = DEALER_TEXTURE + 1;
	public static final int CONTINUE_GAME_TEXTURE = GREEN_GLASS_TEXTURE + 1;
	public static final int SLIDE_ANGLE_BUTTON_TEXTURE = CONTINUE_GAME_TEXTURE + 1;
	public static final int ORDERED_BUTTON = SLIDE_ANGLE_BUTTON_TEXTURE + 1;
	
	
	private Texture mTitleTexture;
	
	private Font mFont;
	
	private TextureRegion mTitleDeck;
	private TextureRegion mTitleEuchreClear;
	private TextureRegion mTitleEuchre;
	private TextureRegion mTextureClub;
	private TextureRegion mTextureDiamond;
	private TextureRegion mTextureHeart;
	private TextureRegion mTextureSpade;
	private TextureRegion mNewGame;
	private TextureRegion mContinueGame;
	private TextureRegion mSettings;
	private TextureRegion mExit;
	private TextureRegion mButton;
	private TextureRegion mTableBlack;
	private TextureRegion mPass;
	private TextureRegion mPickUp;
	private TextureRegion mDealer;
	private TextureRegion mGreenGlass;
	private TextureRegion mOrdered;
	
	private TiledTextureRegion mTiledMissing;
	
	private TiledTextureRegion mTiledClubNine;
	private TiledTextureRegion mTiledClubTen;
	private TiledTextureRegion mTiledClubJack;
	private TiledTextureRegion mTiledClubKing;
	private TiledTextureRegion mTiledClubQueen;
	private TiledTextureRegion mTiledClubAce;
	
	private TiledTextureRegion mTiledDiamondNine;
	private TiledTextureRegion mTiledDiamondTen;
	private TiledTextureRegion mTiledDiamondJack;
	private TiledTextureRegion mTiledDiamondKing;
	private TiledTextureRegion mTiledDiamondQueen;
	private TiledTextureRegion mTiledDiamondAce;
	
	private TiledTextureRegion mTiledHeartNine;
	private TiledTextureRegion mTiledHeartTen;
	private TiledTextureRegion mTiledHeartJack;
	private TiledTextureRegion mTiledHeartKing;
	private TiledTextureRegion mTiledHeartQueen;
	private TiledTextureRegion mTiledHeartAce;
	
	private TiledTextureRegion mTiledSpadeNine;
	private TiledTextureRegion mTiledSpadeTen;
	private TiledTextureRegion mTiledSpadeJack;
	private TiledTextureRegion mTiledSpadeKing;
	private TiledTextureRegion mTiledSpadeQueen;
	private TiledTextureRegion mTiledSpadeAce;
	
	private TiledTextureRegion mSuitsTiled;
	private TiledTextureRegion mSlideAngleButton;
	
	public Textures(Engine pEngine){	
		log("Init textures");
		UI.setLoadState("Initializing Textures");
		mTitleTexture = new Texture(1024, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		UI.setLoadState("Init Club Texture");
		Texture mClubTexture= new Texture(1024,1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		UI.setLoadState("Init Diamond Texture");
		Texture mDiamondTexture= new Texture(1024,1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		UI.setLoadState("Init Heart Texture");
		Texture mHeartTexture= new Texture(1024,1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		UI.setLoadState("Init Spade Texture");
		Texture mSpadeTexture= new Texture(1024,1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		UI.setLoadState("Init Font Texture");
		Texture mFontTexture = new Texture(256, 256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		UI.setLoadState("Init UI Texture");
		Texture mUITexture = new Texture(1024,2048, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		
		
		UI.setLoadState("Loading Textures");
		mNewGame = TextureRegionFactory.createFromAsset(mUITexture, UI.GetGame(), "buttons/new_game.png", 0, 0);//174x144
		UI.setLoadState("Loading Textures.");
		mSettings = TextureRegionFactory.createFromAsset(mUITexture, UI.GetGame(), "buttons/settings.png", 175, 0);//61x59
		UI.setLoadState("Loading Textures..");
		mExit = TextureRegionFactory.createFromAsset(mUITexture, UI.GetGame(), "buttons/exit.png", 250, 0);//199x96
		UI.setLoadState("Loading Textures...");
		mButton = TextureRegionFactory.createFromAsset(mUITexture,UI.GetGame(), "buttons/black_gloss.png",450,0);//357x70
		UI.setLoadState("Loading Textures....");
		mTableBlack = TextureRegionFactory.createFromAsset(mUITexture,UI.GetGame(),"tables/green_empty.png",0,150);//264x270
		UI.setLoadState("Loading Textures.....");
		mTextureDiamond = TextureRegionFactory.createFromAsset(mUITexture,UI.GetGame(),"buttons/suit_diamond.png",265,150);//188x277
		UI.setLoadState("Loading Textures......");
		mTextureClub = TextureRegionFactory.createFromAsset(mUITexture,UI.GetGame(),"buttons/suit_club.png",455,150);//188x277
		UI.setLoadState("Loading Textures.......");
		mTextureHeart = TextureRegionFactory.createFromAsset(mUITexture,UI.GetGame(),"buttons/suit_heart.png",700,150);//188x277
		UI.setLoadState("Loading Textures........");
		mTextureSpade = TextureRegionFactory.createFromAsset(mUITexture,UI.GetGame(),"buttons/suit_spade.png",0,430);//188x277
		UI.setLoadState("Loading Textures.........");
		mSuitsTiled = TextureRegionFactory.createTiledFromAsset(mUITexture, UI.GetGame(), "buttons/suits_tiled.png", 230, 430, 5, 1);
		UI.setLoadState("Loading Textures..........");
		mPass = TextureRegionFactory.createFromAsset(mUITexture,UI.GetGame(),"buttons/pass.png",0,710);
		UI.setLoadState("Loading Textures...........");
		mPickUp = TextureRegionFactory.createFromAsset(mUITexture,UI.GetGame(),"buttons/pickup.png",123,710);//122x98
		
		mDealer = TextureRegionFactory.createFromAsset(mUITexture,UI.GetGame(), "dealer.png", 123,810);//42x42
		
		mGreenGlass = TextureRegionFactory.createFromAsset(mUITexture, UI.GetGame(), "glass_green.png", 246, 710);//180x252
		
		mContinueGame = TextureRegionFactory.createFromAsset(mUITexture, UI.GetGame(), "buttons/continue.png", 430,710);//174x144
		
		mSlideAngleButton = TextureRegionFactory.createTiledFromAsset(mUITexture, UI.GetGame(), "buttons/slide_angle_tiled.png", 0, 1000, 2, 1);//512x256
		
		mOrdered = TextureRegionFactory.createFromAsset(mUITexture, UI.GetGame(), "ordered.png", 513,1000);
		
		UI.setLoadState("Add TitleDeck");
		mTitleDeck = TextureRegionFactory.createFromAsset(mTitleTexture, UI.GetGame(), "title_deck.png", 0, 0);//180*100
		UI.setLoadState("Add TitleEuchre");
		mTitleEuchre = TextureRegionFactory.createFromAsset(mTitleTexture, UI.GetGame(), "title_euchreu_clear.png", 190, 0);//200x200
		UI.setLoadState("Add TitleEuchreClear");
		mTitleEuchreClear = TextureRegionFactory.createFromAsset(mTitleTexture, UI.GetGame(), "title_euchreu.png", 400, 0);//200x200
		
		
		
		
		UI.setLoadState("Loading Ace of Clubs");
		mTiledClubAce = TextureRegionFactory.createTiledFromAsset(mClubTexture, UI.GetGame(), "cards/ca.png",0,0,2,1); //Ends at 360,252
		UI.setLoadState("Loading King of Clubs");
		mTiledClubKing = TextureRegionFactory.createTiledFromAsset(mClubTexture, UI.GetGame(), "cards/ck.png",400,0,2,1); //Ends at 760, 0
		UI.setLoadState("Loading Queen of Clubs");
		mTiledClubQueen = TextureRegionFactory.createTiledFromAsset(mClubTexture, UI.GetGame(), "cards/cq.png",0,256,2,1);
		UI.setLoadState("Loading Jack of Clubs");
		mTiledClubJack = TextureRegionFactory.createTiledFromAsset(mClubTexture, UI.GetGame(), "cards/cj.png",400,256,2,1);
		UI.setLoadState("Loading 10 of Clubs");
		mTiledClubTen = TextureRegionFactory.createTiledFromAsset(mClubTexture, UI.GetGame(), "cards/c10.png",0,512,2,1);
		UI.setLoadState("Loading 9 of Clubs");
		mTiledClubNine = TextureRegionFactory.createTiledFromAsset(mClubTexture, UI.GetGame(), "cards/c9.png",400,512,2,1);

		UI.setLoadState("Loading Ace of Diamonds");
		mTiledDiamondAce = TextureRegionFactory.createTiledFromAsset(mDiamondTexture, UI.GetGame(), "cards/da.png",0,0,2,1);
		UI.setLoadState("Loading King of Diamonds");
		mTiledDiamondKing = TextureRegionFactory.createTiledFromAsset(mDiamondTexture, UI.GetGame(), "cards/dk.png",512,0,2,1);
		UI.setLoadState("Loading Queen of Diamonds");
		mTiledDiamondQueen = TextureRegionFactory.createTiledFromAsset(mDiamondTexture, UI.GetGame(), "cards/dq.png",0,256,2,1);
		UI.setLoadState("Loading Jack of Diamonds");
		mTiledDiamondJack = TextureRegionFactory.createTiledFromAsset(mDiamondTexture, UI.GetGame(), "cards/dj.png",512,256,2,1);
		UI.setLoadState("Loading 10 of Diamonds");
		mTiledDiamondTen = TextureRegionFactory.createTiledFromAsset(mDiamondTexture, UI.GetGame(), "cards/d10.png",0,512,2,1);
		UI.setLoadState("Loading 9 of Diamonds");
		mTiledDiamondNine = TextureRegionFactory.createTiledFromAsset(mDiamondTexture, UI.GetGame(), "cards/d9.png",512,512,2,1);
		
		UI.setLoadState("Loading Ace of Hearts");
		mTiledHeartAce = TextureRegionFactory.createTiledFromAsset(mHeartTexture, UI.GetGame(), "cards/ha.png",0,0,2,1);
		UI.setLoadState("Loading King of Hearts");
		mTiledHeartKing = TextureRegionFactory.createTiledFromAsset(mHeartTexture, UI.GetGame(), "cards/hk.png",512,0,2,1);
		UI.setLoadState("Loading Queen of Hearts");
		mTiledHeartQueen = TextureRegionFactory.createTiledFromAsset(mHeartTexture, UI.GetGame(), "cards/hq.png",0,256,2,1);
		UI.setLoadState("Loading Jack of Hearts");
		mTiledHeartJack = TextureRegionFactory.createTiledFromAsset(mHeartTexture, UI.GetGame(), "cards/hj.png",512,256,2,1);
		UI.setLoadState("Loading 10 of Hearts");
		mTiledHeartTen = TextureRegionFactory.createTiledFromAsset(mHeartTexture, UI.GetGame(), "cards/h10.png",0,512,2,1);
		UI.setLoadState("Loading 9 of Hearts");
		mTiledHeartNine = TextureRegionFactory.createTiledFromAsset(mHeartTexture, UI.GetGame(), "cards/h9.png",512,512,2,1);
		
		UI.setLoadState("Loading Ace of Spades");
		mTiledSpadeAce = TextureRegionFactory.createTiledFromAsset(mSpadeTexture, UI.GetGame(), "cards/sa.png",0,0,2,1);
		UI.setLoadState("Loading King of Spades");
		mTiledSpadeKing = TextureRegionFactory.createTiledFromAsset(mSpadeTexture, UI.GetGame(), "cards/sk.png",512,0,2,1);
		UI.setLoadState("Loading Queen of Spades");
		mTiledSpadeQueen = TextureRegionFactory.createTiledFromAsset(mSpadeTexture, UI.GetGame(), "cards/sq.png",0,256,2,1);
		UI.setLoadState("Loading Jack of Spades");
		mTiledSpadeJack = TextureRegionFactory.createTiledFromAsset(mSpadeTexture, UI.GetGame(), "cards/sj.png",512,256,2,1);
		UI.setLoadState("Loading 10 of Spades");
		mTiledSpadeTen = TextureRegionFactory.createTiledFromAsset(mSpadeTexture, UI.GetGame(), "cards/s10.png",0,512,2,1);
		UI.setLoadState("Loading 9 of Spades");
		mTiledSpadeNine = TextureRegionFactory.createTiledFromAsset(mSpadeTexture, UI.GetGame(), "cards/s9.png",512,512,2,1);
		
		UI.setLoadState("Loading Font");
		mFont = new Font(mFontTexture, Typeface.create(
				Typeface.DEFAULT, Typeface.BOLD), 12, true, Color.GREEN);
		
		
		log("Loading Textures");
		UI.setLoadState("Finalize Textures");
		pEngine.getTextureManager().loadTextures(mClubTexture,mDiamondTexture,mHeartTexture,mSpadeTexture,mUITexture,mFontTexture,mTitleTexture);
		UI.setLoadState("Finalize Font");
		pEngine.getFontManager().loadFont(mFont);
		log("Textures Loaded");
	}
	
	public TextureRegion getTexture(int pTextureID){
		switch(pTextureID){
		case TITLE_DECK_TEXTURE:
			return mTitleDeck.clone();
		case TITLE_EUCHRE_TEXTURE:
			return mTitleEuchre.clone();
		case TITLE_EUCHRE_CLEAR_TEXTURE:
			return mTitleEuchreClear.clone();
		case NEW_GAME_TEXTURE:
			return mNewGame.clone();
		case CONTINUE_GAME_TEXTURE:
			return mContinueGame.clone();
		case SETTINGS_TEXTURE:
			return mSettings.clone();
		case EXIT_TEXTURE:
			return mExit.clone();
		case BUTTON_TEXTURE:
			return mButton.clone();
		case TABLE_BLACK_TEXTURE:
			return mTableBlack.clone();
		case CLUB_TEXTURE:
			return mTextureClub.clone();
		case DIAMOND_TEXTURE:
			return mTextureDiamond.clone();
		case HEART_TEXTURE:
			return mTextureHeart.clone();
		case SPADE_TEXTURE:
			return mTextureSpade.clone();
		case PICK_UP_TEXTURE:
			return mPickUp.clone();
		case PASS_TEXTURE:
			return mPass.clone();
		case DEALER_TEXTURE:
			return mDealer.clone();
		case GREEN_GLASS_TEXTURE:
			return mGreenGlass.clone();
		case ORDERED_BUTTON:
			return mOrdered.clone();
		default:
			return null;
		}
	}
	
	public TiledTextureRegion getTiledSuits(){
		return mSuitsTiled.clone();
	}
	
	public TiledTextureRegion getTiledSlideButton(){
		return mSlideAngleButton.clone();
	}
	
	public TiledTextureRegion getCardTiledTextureRegion(int pSuitID, int pValueID)
	{
		switch(pSuitID)
		{
		case Suits.CLUBS:
			switch(pValueID)
			{
			case CardValues.ACE:
				return mTiledClubAce.clone();
			case CardValues.KING:
				return mTiledClubKing.clone();
			case CardValues.QUEEN:
				return mTiledClubQueen.clone();
			case CardValues.JACK:
				return mTiledClubJack.clone();
			case CardValues.TEN:
				return mTiledClubTen.clone();
			case CardValues.NINE:
				return mTiledClubNine.clone();
			default:
				return mTiledMissing.clone();
			}
		case Suits.DIAMONDS:
			switch(pValueID)
			{
			case CardValues.ACE:
				return mTiledDiamondAce.clone();
			case CardValues.KING:
				return mTiledDiamondKing.clone();
			case CardValues.QUEEN:
				return mTiledDiamondQueen.clone();
			case CardValues.JACK:
				return mTiledDiamondJack.clone();
			case CardValues.TEN:
				return mTiledDiamondTen.clone();
			case CardValues.NINE:
				return mTiledDiamondNine.clone();
			default:
				return mTiledMissing.clone();
			}
		case Suits.HEARTS:
			switch(pValueID)
			{
			case CardValues.ACE:
				return mTiledHeartAce.clone();
			case CardValues.KING:
				return mTiledHeartKing.clone();
			case CardValues.QUEEN:
				return mTiledHeartQueen.clone();
			case CardValues.JACK:
				return mTiledHeartJack.clone();
			case CardValues.TEN:
				return mTiledHeartTen.clone();
			case CardValues.NINE:
				return mTiledHeartNine.clone();
			default:
				return mTiledMissing.clone();
			}
		case Suits.SPADES:
			switch(pValueID)
			{
			case CardValues.ACE:
				return mTiledSpadeAce.clone();
			case CardValues.KING:
				return mTiledSpadeKing.clone();
			case CardValues.QUEEN:
				return mTiledSpadeQueen.clone();
			case CardValues.JACK:
				return mTiledSpadeJack.clone();
			case CardValues.TEN:
				return mTiledSpadeTen.clone();
			case CardValues.NINE:
				return mTiledSpadeNine.clone();
			default:
				return mTiledMissing.clone();
			}
			default:
				return mTiledMissing.clone();
		}
	}
	
	public float getHeightFromWidth(int pTextureID, float pWidth){
		return getHeightFromWidth(getTexture(pTextureID),pWidth);
		
	}
	
	public float getHeightFromWidth(TextureRegion tr, float pWidth){
		float x = pWidth / tr.getWidth();
		return tr.getHeight() * x;
	}
	
	public float getHeightFromWidthTiled(int pSuitID, int pValueID, float pWidth){
		return getHeightFromWidthTiled(
				getCardTiledTextureRegion(pSuitID,pValueID),pWidth);
	}
	
	public float getHeightFromWidthTiled(TiledTextureRegion tr, float pWidth){
		if(tr == null)return -1;
		float x = pWidth / tr.getTileWidth();
		
		return tr.getTileHeight() * x;
	}
	
	public Font getFont(){
		return mFont;
	}
	
	private void log(String msg){
		Log.d("Textures", msg);
	}
	
}
